import { router , addRoutes } from '~/router'
import { getToken } from '~/composables/auth.js'
import { toast,showFullLoading,hideFullLoading } from '~/composables/util.js'
import store from './store';
//全局前置守卫
let hasGetInfo = false;
router.beforeEach(async (to, from, next) => {
    //显示loading
    showFullLoading();
    const token = getToken();
    //没有登录强制跳转登录页
    if(!token && to.path !== "/login"){
        toast("请先登录","error");
        return next({path:"/login"});
    }
    //防止重复登录
    if(token && to.path == "/login"){
        toast("请勿重复登录","error");
        return next({path:from.path ? from.path : "/"});
    }
    //如果用户登录，自动获取用户信息，并存储在vuex中
    let hasNewRoutes = false;
     if(token && !hasGetInfo){
        let { menus } = await store.dispatch('getInfo');
        hasGetInfo = true;
        //动态添加路由
        hasNewRoutes =  addRoutes(menus);
    }
    //设置页面的标题
    let title = (to.meta.title ?  to.meta.title : "") + "-vue3后台管理";
    document.title = title;
    hasNewRoutes ?  next(to.fullPath) : next();
})

//全局后置钩子
router.afterEach((to,from) => hideFullLoading());